package cn.iocoder.yudao.module.system.dal.mysql.merchant;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
import cn.iocoder.yudao.module.system.controller.admin.merchant.vo.MerchantApplicationPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.merchant.vo.MerchantAuthUpdateReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.merchant.SystemMerchantAuth;
import cn.iocoder.yudao.module.system.enums.merchant.CertificationEnum;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.ibatis.annotations.Mapper;

import java.util.Objects;

@Mapper
public interface MerchantAuthMapper extends BaseMapperX<SystemMerchantAuth> {

    default PageResult<SystemMerchantAuth> selectPageByReqVO(MerchantApplicationPageReqVO reqVO) {
        LambdaQueryWrapperX<SystemMerchantAuth> query = new LambdaQueryWrapperX<>();
        if(!Objects.isNull(reqVO.getCertificationDate())&&reqVO.getCertificationDate().length == 2){
            query.between(SystemMerchantAuth::getCertificationDate, reqVO.getCertificationDate()[0], reqVO.getCertificationDate()[1]);
        }
        query.likeIfPresent(SystemMerchantAuth::getMerchantName, reqVO.getMerchantName())
                .likeIfPresent(SystemMerchantAuth::getLegalRepresentative, reqVO.getLegalRepresentative())
                .likeIfPresent(SystemMerchantAuth::getContactPhone, reqVO.getContactPhone())
                .eqIfPresent(SystemMerchantAuth::getIdentify, reqVO.getIdentify())
                .eqIfPresent(SystemMerchantAuth::getCertificationStatus, reqVO.getCertificationStatus());
        return selectPage(reqVO,query);
    }


    default SystemMerchantAuth selectByUserIdAndId(MerchantAuthUpdateReqVO reqVO) {
        LambdaQueryWrapperX<SystemMerchantAuth> query = new LambdaQueryWrapperX<>();
        query.eq(SystemMerchantAuth::getUserId, reqVO.getUserId())
                .eq(SystemMerchantAuth::getId, reqVO.getId());
        return selectOne(query);
    }

    default boolean rejectMerchantAuth(MerchantAuthUpdateReqVO reqVO) {
        LambdaUpdateWrapper<SystemMerchantAuth> update = new LambdaUpdateWrapper<>();
        update.eq(SystemMerchantAuth::getId, reqVO.getId())
                .eq(SystemMerchantAuth::getUserId, reqVO.getUserId())
                .set(SystemMerchantAuth::getCertificationStatus, CertificationEnum.FAIL.getStatus());
        return update(update) > 0;
    }

    default boolean passedMerchantAuth(MerchantAuthUpdateReqVO reqVO) {
        LambdaUpdateWrapper<SystemMerchantAuth> update = new LambdaUpdateWrapper<>();
        update.eq(SystemMerchantAuth::getId, reqVO.getId())
                .eq(SystemMerchantAuth::getUserId, reqVO.getUserId())
                .set(SystemMerchantAuth::getCertificationStatus, CertificationEnum.SUCCESS.getStatus())
                        .set(SystemMerchantAuth::getHandler, WebFrameworkUtils.getLoginUserId());
        return update(update) > 0;
    }

}
